package com.transformer.shobal.activitys.wigets;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.transformer.shobal.R;

import java.lang.ref.WeakReference;

/**
 * Created by Administrator on 2015/2/2.
 */
public class PullDownView extends RelativeLayout{
    WeakReference<Context> mContextHolder;
    private float mLastY;
    private ImageView pullView;
    private int[] locations = new int[2];
    private int minHeight;
    private int defaultHeight;
    private Matrix matrix = new Matrix();
    private Bitmap bitmap;
    private Bitmap bitmap1;
    private NinePatch np;

    public PullDownView(Context context) {
        super(context);
        mContextHolder = new WeakReference<Context>(context);
        initView();
    }

    public PullDownView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContextHolder = new WeakReference<Context>(context);
        initView();
    }

    public PullDownView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContextHolder = new WeakReference<Context>(context);
        initView();
    }

    private void initView(){
        minHeight = 200;
        defaultHeight = 400;
        pullView = new ImageView(mContextHolder.get());
        pullView.setBackgroundResource(R.drawable.star_swing_10);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.height = defaultHeight;
        layoutParams.width = 240;
        pullView.setLayoutParams(layoutParams);
        addView(pullView);
        bitmap = BitmapFactory.decodeResource(mContextHolder.get().getResources(),R.drawable.star_swing_10);
        bitmap1 = BitmapFactory.decodeResource(mContextHolder.get().getResources(),R.drawable.ic_launcher);
        np = new NinePatch(bitmap,bitmap.getNinePatchChunk(),null);
    }

    public int getMinHeight() {
        return minHeight;
    }

    public void setMinHeight(int minHeight) {
        this.minHeight = minHeight;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint demoPaint = new Paint();
        demoPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
        demoPaint.setColor(Color.RED);
        demoPaint.setStyle(Paint.Style.STROKE);
        canvas.drawCircle(getWidth()/2, getHeight()/2,50,demoPaint);
        canvas.drawCircle(getWidth()/2, getHeight()/2-defaultHeight/2,50,demoPaint);

        Log.i("shobal", "ondraw-----------");
        matrix.reset();
//        matrix.setTranslate(100,20);
        matrix.setScale(1,(bitmap1.getHeight()+200)/bitmap.getHeight());
//        matrix.setRotate(20, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
        canvas.drawBitmap(bitmap1,matrix,null);
        canvas.rotate(-45,bitmap.getWidth()/2,0);
        np.draw(canvas,new Rect(100,0,300,400));
//        canvas.drawBitmap(bitmap,20,20,demoPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        pullView.getLocationInWindow(locations);
        pullView.setPivotX(pullView.getWidth()/2);
        pullView.setPivotY(0);
        Log.i("shobal","event.getY()="+event.getY()+",event.getX()="+event.getX()+",locations[0]="+locations[0]+
                ",locations[1]="+locations[1]+",pullView.getHeight()="+pullView.getHeight()+",pullView.getWidth()="+pullView.getWidth());
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mLastY = event.getY();
                return  true;
            case MotionEvent.ACTION_MOVE:
                //拉伸
/*                int height = (int)(event.getY()-mLastY);
                mLastY = event.getY();
                pullView.getLayoutParams().height += height;
                if(pullView.getLayoutParams().height < minHeight)
                    pullView.getLayoutParams().height = minHeight;*/

                //旋转
 /*               double a = 0;
                if(event.getX() <=0 ){
                    a = getWidth()/2 + Math.abs(event.getX());
                }else{
                    a = getWidth()/2 - event.getX();
                }
                if(a == 0)
                    break ;
                double c = Math.sqrt(Math.pow(a,2)+Math.pow(getY(),2));
                double degree = Math.asin(a/c)*180;
                pullView.setRotation((float) degree);
                Log.i("shobal","MotionEvent.ACTION_MOVE degree="+degree+",a="+a+",c="+c+",getWidth()/2="+getWidth()/2);*/
                requestLayout();
                break;
            case MotionEvent.ACTION_UP:
//                Animation animation = AnimationUtils.loadAnimation(mContextHolder.get(),R.anim.tantiao);
/*
                Animation animation = new ScaleAnimation(1f,1f,1f,0.3f,0.8f,0.5f);
                animation.setInterpolator(new BounceInterpolator());
                animation.setDuration(2000);
                animation.setRepeatCount(1000);
                pullView.setAnimation(animation);
                animation.start();*/
/*                ObjectAnimator o = ObjectAnimator.ofInt(pullView,"height",500);
                o.setInterpolator(new BounceInterpolator());
                o.setRepeatCount(100);
                o.setDuration(1000);
                o.start();*/
/*                mLastY = pullView.getLayoutParams().height;
                ValueAnimator v = ValueAnimator.ofInt(1,100);
                v.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

                    //持有一个IntEvaluator对象，方便下面估值的时候使用
                    private IntEvaluator mEvaluator = new IntEvaluator();

                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        //获得当前动画的进度值，整型，1-100之间
                        int currentValue = (Integer) animator.getAnimatedValue();

                        //计算当前进度占整个动画过程的比例，浮点型，0-1之间
                        float fraction = currentValue / 100f;

                        //这里我偷懒了，不过有现成的干吗不用呢
                        //直接调用整型估值器通过比例计算出宽度，然后再设给Button
                        pullView.getLayoutParams().height = mEvaluator.evaluate(fraction, (int)mLastY, minHeight);
                        pullView.requestLayout();
                    }
                });
                v.setInterpolator(new BounceInterpolator());
                v.setDuration(1000);
                v.start();*/
                break;
        }
        return super.onTouchEvent(event);
    }
}
